﻿Public Class frmCodeManager
    Private o_Code As CodeStore = Nothing
    Private IsNewItem As Boolean = False
    Private IsDirty As Boolean = False
    ''' <summary>
    ''' Clear Form
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearForm()
        Me.txtCode.Text = ""
        Me.txtCode2.Text = ""
        Me.txtDescription.Text = ""
        Me.txtExample.Text = ""
        Me.txtName.Text = ""
        Me.txtNameSpace.Text = ""
        Me.txtRequire.Text = ""
        o_Code = Nothing
    End Sub
    Private Sub frmCodeManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CodeTree1.LoadData(My.Application.Info.DirectoryPath & "\Data\Java\Java.xml")
    End Sub
    Private Sub cmdOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdOK.Click
        CodeTree1.CodeStoreCollection.Save(My.Application.Info.DirectoryPath & "\Data\Java\Java.xml")
        'CodeTree1.CodeStoreCollection.Save("f:\Data\Java\Java.xml")
        Me.Hide()
    End Sub
    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
        Me.Close()
    End Sub
    Private Sub PropertyGrid1_PropertyValueChanged(ByVal s As Object, ByVal e As System.Windows.Forms.PropertyValueChangedEventArgs)
        Me.CodeTree1.RefreshData()
    End Sub
    Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
        If cmdAdd.Text = "Save" Then
            'save the data
            'SetObject(Me.o_Code)
            If IsNewItem = True Then
                Me.o_Code = New CodeStore
                SetObject(Me.o_Code)
                IsNewItem = False
                Me.CodeTree1.CodeStoreCollection.Add(Me.o_Code)
            Else
                SetObject(Me.o_Code)
                Me.IsNewItem = False
            End If
            cmdAdd.Text = "New"
            IsDirty = False
            Me.CodeTree1.RefreshData()
            cmdCancelAction.Enabled = False
            'TableLayoutPanel1.Enabled = false
        Else
            IsNewItem = True
            ClearForm() 'clear the form
            cmdAdd.Text = "Save"
            IsDirty = True
            Me.CodeTree1.RefreshData()
            cmdCancelAction.Enabled = True
            TableLayoutPanel1.Enabled = True
        End If

    End Sub
    Private Sub CodeTree1_NodeSelected(ByRef Node As CodeStore) Handles CodeTree1.NodeSelected
        If Me.o_Code IsNot Nothing Then
            If IsDirty = True Then
                Dim r As DialogResult = MsgBox("Save Changes", MsgBoxStyle.YesNo)
                If r = Windows.Forms.DialogResult.Yes Then
                    cmdAdd_Click(Nothing, Nothing)
                End If
            End If
        End If
        'If Me.o_Code IsNot Nothing Then
        '    Me.o_Code = Node
        '    GetObject(Me.o_Code)
        '    cmdAdd.Text = "New"
        'End If
        If Me.o_Code Is Nothing Or Me.IsNewItem = False Then
            Me.o_Code = Node
            GetObject(Me.o_Code)
            cmdAdd.Text = "New"
        ElseIf Me.o_Code IsNot Nothing Then
            Me.o_Code = Node
            GetObject(Me.o_Code)
            cmdAdd.Text = "New"
        End If
        TableLayoutPanel1.Enabled = True
        cmdCancelAction.Enabled = False
        'cmdAdd.Enabled = False
        IsDirty = False
    End Sub
    Private Sub GetObject(ByVal Node As CodeStore)
        Me.txtCode.Text = Node.Code
        Me.txtCode2.Text = Node.Code2
        Me.txtDescription.Text = Node.Description
        Me.txtExample.Text = Node.Example 
        Me.txtName.Text = Node.Name
        Me.txtNameSpace.Text = Node.Name_Space
        Me.txtRequire.Text = Node.RequireLib
    End Sub
    Private Sub SetObject(ByVal Node As CodeStore)
        Node.Code = Me.txtCode.Text
        Node.Code2 = Me.txtCode2.Text
        Node.Description = Me.txtDescription.Text
        Node.Example = Me.txtExample.Text
        Node.Name = Me.txtName.Text
        Node.Name_Space = Me.txtNameSpace.Text
        Node.RequireLib = Me.txtRequire.Text
        TableLayoutPanel1.Enabled = True
    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub

    Private Sub CodeTree1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles CodeTree1.AfterSelect

    End Sub
    ''' <summary>
    ''' Text changed make dirty
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub txtName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtName.TextChanged, txtCode.TextChanged, txtCode2.TextChanged, txtDescription.TextChanged, txtNameSpace.TextChanged, txtRequire.TextChanged, txtExample.TextChanged
        IsDirty = True
        cmdCancelAction.Enabled = True
        cmdAdd.Text = "Save"

        'cmdAdd.Enabled = True
    End Sub

    Private Sub cmdCancelAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelAction.Click
        IsNewItem = False
        ClearForm() 'clear the form
        cmdAdd.Text = "New"
        IsDirty = False
        cmdCancelAction.Enabled = False
        Me.CodeTree1.RefreshData()
        TableLayoutPanel1.Enabled = False
    End Sub
End Class